const getModel = (con) => {
    con = con.replace(/ /g, '')
    con = con.toLowerCase()
    const l = con.length
    // 判断模式2 和 模式4
    if (con.indexOf(',') > -1) {
        const cons = con.split(',')
        if (cons.length === 3 || cons.length === 4) {
            for (let i = 0; i < 3; i++) { // 判断前3位是否是小于255的
                if (Number.isNaN(cons[i]) || parseInt(cons[i], 10) > 255) return { type: 0, con: '' }
            }
            if (cons.length === 3) return { type: 2, con }
            if (parseFloat(cons[3]) <= 1 && parseFloat(cons[3]) >= 0) return { type: 4, con }
            return { type: 0, con: '' }
        }
        return { type: 0, con: '' }
    }
    // 判断模式1
    if (l === 6 || l === 3 || l === 8 || (con.substring(0, 1) === '#' && (l === 7 || l === 4 || l === 9))) {
        // 支持：ffffff  #ffffff  fff #fff   #ff123455 ff123456
        if (l === 7 || l === 4 || l === 9) con = con.substring(1)
        if (con.match(/^[0-9,a-f]+$/i)) {
            if (con.length === 6) return { type: 1, con }
            if (con.length === 3) {
                const red = con.substring(0, 1)
                const green = con.substring(1, 2)
                const blue = con.substring(2, 3)
                return { type: 1, con: red + red + green + green + blue + blue }
            }
            return { type: 3, con }
        }
        return { type: 0, con: '' }
    }
    return { type: 0, con: '' }
}
const setLengthTwo = (str) => {
    if (str.length === 1) { return `0${str}` }
    return str
}

const rgb2hsl = (r, g, b) => {
    r /= 255, g /= 255, b /= 255
    const max = Math.max(r, g, b); const
        min = Math.min(r, g, b)
    let h; let s; let
        l = (max + min) / 2

    if (max == min) {
        h = s = 0 // achromatic
    } else {
        const d = max - min
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
        switch (max) {
        case r:
            h = (g - b) / d + (g < b ? 6 : 0); break
        case g:
            h = (b - r) / d + 2; break
        case b:
            h = (r - g) / d + 4; break
        default:
            break
        }
        h /= 6
    }
    h = `${Math.round(360 * h)}°`
    s = `${(s * 100).toFixed(2)}% `
    l = `${(l * 100).toFixed(2)}% `
    return [h, s, l]
}
const rgb2hsv = (r1, r2, r3) => {
    const maxJ = Math.max(r1, r2, r3)
    const minJ = Math.min(r1, r2, r3)
    const _max = maxJ - minJ
    let H; let S; let V
    if (r1 == r2 && r2 == r3) {
        H = 0
        S = 0
    } else {
        switch (maxJ) {
        case r1:
            H = (r2 - r3) / _max
            break
        case r2:
            H = 2 + (r3 - r1) / _max
            break
        case r3:
            H = 4 + (r1 - r2) / _max
            break
        default:
            break
        }
        H *= 60
        if (H < 0) H += 360
        H = (H).toFixed(2)
        S = ((maxJ - minJ) / maxJ).toFixed(2)
    }
    V = (maxJ / 255).toFixed(2)
    return [H, S, V]
}
const parseColor = (con, model) => {
    let a = 0
    let r = 0
    let g = 0
    let b = 0
    const cons = con.split(',')
    switch (model) {
    case 1:
        r = parseInt(con.substring(0, 2), 16)
        g = parseInt(con.substring(2, 4), 16)
        b = parseInt(con.substring(4, 6), 16)
        a = 1
        break
    case 2:
        r = Number(cons[0])
        g = Number(cons[1])
        b = Number(cons[2])
        a = 1
        break
    case 3:
        r = parseInt(con.substring(2, 4), 16)
        g = parseInt(con.substring(4, 6), 16)
        b = parseInt(con.substring(6, 8), 16)
        a = Number((parseInt(con.substring(0, 2), 16) / 255).toFixed(3))
        break
    case 4:
        r = Number(cons[0])
        g = Number(cons[1])
        b = Number(cons[2])
        a = parseFloat(cons[3])
        break
    default:
        break
    }
    // debug('a = ' + a);
    // debug('r = ' + r);
    // debug('g = ' + g);
    // debug('b = ' + b);
    const re = []

    const cr = parseInt((255 * (1 - a) + r * a).toString(), 10)
    const cg = parseInt((255 * (1 - a) + g * a).toString(), 10)
    const cb = parseInt((255 * (1 - a) + b * a).toString(), 10)

    const cr16 = setLengthTwo(cr.toString(16))
    const cg16 = setLengthTwo(cg.toString(16))
    const cb16 = setLengthTwo(cb.toString(16))
    re[0] = (`#${cr16}${cg16}${cb16}`).toUpperCase()
    re[1] = `rgb(${cr},${cg},${cb})`

    const aa = setLengthTwo(parseInt((a * 255).toString(), 10).toString(16))
    const ar = setLengthTwo(parseInt((r).toString(), 10).toString(16))
    const ag = setLengthTwo(parseInt((g).toString(), 10).toString(16))
    const ab = setLengthTwo(parseInt((b).toString(), 10).toString(16))

    re[2] = (`#${aa}${ar}${ag}${ab}`).toUpperCase()
    re[3] = `rgba(${r},${g},${b},${a})`
    // debug(re[0]);
    // debug(re[1]);
    // debug(re[2]);
    // debug(re[3]);

    const hsl = rgb2hsl(cr, cg, cb)
    re[4] = `hsl(${hsl[0]}, ${hsl[1]}, ${hsl[2]})`

    const hsv = rgb2hsv(cr, cg, cb)
    re[5] = `hsv(${hsv[0]}, ${hsv[1]}, ${hsv[2]})`
    return re
}

const colorTransfer = (color) => {
    console.log(color)

    if (!color) return []
    let color1 = color.replace(/\(|\)|rgb|rgba| /g, '')
    color1 = color1.toLowerCase()
    const { type, con } = getModel(color1)

    if (type === 0) {
        console.warn('颜色输入错误')
        return []
    }
    const re = parseColor(con, type)
    console.log(re)

    const obj = {
        '16进制': re[0],
        RGB: re[1],
        'ARGB(android常用)': re[2],
        'RGBA(HTML5、CSS3常用)': re[3],
        HSL: re[4],
        HSV: re[5]
    }
    console.table(obj)
    return re

    // $('#colorbox1').css('background-color', re[0]) // 16进制
    // $('#colorbox2').css('background-color', re[1]) // RGB
    // $('#colorbox3').css({ 'background-color': re[3] }) // ARGB(android常用)
    // $('#colorbox4').css('background-color', re[3]) // RGBA(HTML5、CSS3常用)
    // $('#colorbox5').css('background-color', re[1]) // HSL
    // $('#colorbox6').css('background-color', re[0]) // HSV
}
export default colorTransfer
